package com.haredot.mapper;

import com.haredot.dto.FuncDto;
import com.haredot.entity.Func;
import com.haredot.entity.Menu;
import com.haredot.vo.FuncVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;

import java.util.List;
import java.util.Map;

@Mapper
public interface FuncMapper {


    List<FuncDto> findFuncsByCondtion(Map<String, Object> params);

    Func findFuncByName(String funcName);

    @CacheEvict(cacheNames = "func:all", allEntries = true)
    int insert(FuncVo func);

    int batchSaveMenusByFuncId(@Param("funcId") Long funcId, @Param("menus") List<Long> menus);


    @Caching(cacheable = {
        @Cacheable(value = "func:menus:ref", key = "#funcId", condition = "#funcId != null"),
        @Cacheable(value = "func:menus:ref", key = "", condition = "#funcId == null")
    })
    List<Menu> findMenusRefFuncId(Long funcId);

    @CacheEvict(cacheNames = "func:all", allEntries = true)
    int batchRemoveByFuncId(List<Long> funcIds);

    @CacheEvict(cacheNames = "func:all", allEntries = true)
    int deleteById(@Param("id") Long funcId);

    @CacheEvict(cacheNames = "func:all", allEntries = true)
    int update(FuncVo funcVo);

    @CacheEvict(cacheNames = "func:menus:ref", key = "#a0")
    int batchRemoveMenuAndFuncByFunc(@Param("funcId") Long funcId);
}